package com.kc.sjwh.action;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Cfg;
import com.kc.sjwh.other.Dict;
import com.kc.sjwh.other.MyToken;
import com.kc.sjwh.service.CzllyService;
import com.kc.sjwh.service.CzllzService;
import com.kc.sjwh.service.CzllzcyService;
import com.kc.sjwh.service.SysService;

/**
 * 系统功能action
 * @author zhangguihua(hua7381@163.com)
 * date: 2017年7月19日
 */
@Controller
@RequestMapping("sys")
public class SysAction extends BaseAction {
	static Logger logger = Logger.getLogger(SysAction.class);

	@Autowired
	SysService sysService;
	@Autowired
	CzllyService czllyService;
	@Autowired
	CzllzService czllzService;
	@Autowired
	CzllzcyService czllzcyService;
	
	@RequestMapping("browser")
	public String browser() {
		return "sys/browser";
	}

	/**
	 * 打开登录
	 * @return
	 */
	@RequestMapping("login")
	public String login(HttpServletRequest req, HttpServletResponse resp) {
		req.getSession().setAttribute("webTitle", Cfg.getInst().getWebTitle());
		if(super.isAjax(req)) {//ajax请求时的未登录处理
			AjaxRes res = new AjaxRes();
			res.setStatus("401");
			res.setErrMsg("长时间未操作，请重新登录");
			super.write(resp, JSON.toJSONString(res));
			return null;
		} else {
			return "sys/login";
		}
	}
	
	/**
	 * 登出
	 * @return
	 */
	@RequestMapping("logout")
	public String logout(HttpServletRequest req) {
		SecurityUtils.getSubject().logout();
		req.getSession().setAttribute("webTitle", Cfg.getInst().getWebTitle());
		return "sys/logout";
	}
	
	/**
	 * 登录
	 * @param acc
	 * @param pwd
	 * @return
	 */
	@ResponseBody
	@RequestMapping("doLogin")
	public String doLogin(String acc, String pwd) {
		SecurityUtils.getSubject().login(new MyToken(acc, pwd, true));
		logger.info("登录成功,"+acc);
		return JSON.toJSONString(new AjaxRes());
	}

	/**
	 * 加载菜单，并存到session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("loadMenusHtml")
	public String loadMenusHtml() {
		String h = sysService.loadMenusHtml(getLoginUser().getId());
		SecurityUtils.getSubject().getSession().setAttribute("menusHtml", h);
		return JSON.toJSONString(new AjaxRes(h));
	}
	
	/**
	 * 打开树型选择器
	 * @param req
	 * @param treeType 树的类别
	 * @param extra 额外参数
	 * @return
	 */
	@RequestMapping("tree")
	public String tree(HttpServletRequest req, String treeType, String extra) {
		if("sex".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(Dict.getSex().getList()));
		} else if("ryzw".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(Dict.getRyzw().getList()));
		} 
		
		else if("organ".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(Dict.getOrgan4Choose().getList()));
		}
		else if("organ_nb".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllNbdwAsDict()));
		}else if("jqxz".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(Dict.getJqxz().getList()));
		} 
		
		else if("pdtthzlx".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(Dict.getPdtthzlx().getList()));
		} else if("pdtsblx".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(Dict.getPdtsblx().getList()));
		}
		
		else if("app".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllAppAsDict()));
		} else if("res".equals(treeType)) {
			String yyid = extra;//所属应用id
			req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllResAsDict(yyid)));
		} else if("role".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllRoleAsDict()));
		}
		
		else if("czlly".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(czllyService.getAllAsDict()));
		} else if("czllz".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(czllzService.getAllAsDict()));
		} else if("czllzcy".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(czllzcyService.getAllAsDict()));
			return "sys/cytree";
		}
		 //警情标注字典资源树
		 else if("jqbz".equals(treeType)) {
			    String bzbh = (String) req.getSession().getAttribute("bzbh");
				req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllJqbzzdAsDict(bzbh)));
			}
		//警情标注类别字典
		 else if("jqbzlb".equals(treeType)) {
				req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllJqbzlbDict()));
		}
		//资源库-资源类别字典集
		 else if("zykzd".equals(treeType)) {
				req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllZykzdDict()));
		}
		//指挥中心
		 else if("zhzx".equals(treeType)) {
				req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllZhzxDict()));
		}
		//指挥中心配置指标项
		 else if("zhzxzbx".equals(treeType)) {
				req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllPzxDict()));
		}
		else if("dict".equals(treeType)) {
			String zdlb="XXX";
			try {
				zdlb = new String(extra.getBytes("ISO-8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//字典类别
			req.setAttribute("treeJson", JSON.toJSONString(sysService.getDictByType(zdlb)));
		}else if("zdssz".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllZdsszAsDict()));
		}else if("gztz".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllGztzAsDict()));
		}else if("zhzx".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllZhzxAsDict()));
		}else if("organ_m".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllNbdwAsDict()));
			return "sys/tree_m";
		} else if("gztpzx".equals(treeType)) {
			req.setAttribute("treeJson", JSON.toJSONString(sysService.getAllGztpzxAsDict()));
		}
		return "sys/tree";
		
	}
	
	@RequestMapping("403")
	public String _403(HttpServletRequest req, HttpServletResponse resp) {
		if(super.isAjax(req)) {
			AjaxRes res = new AjaxRes();
			res.setStatus("403");
			res.setErrMsg("未授权访问");
			super.write(resp, JSON.toJSONString(res));
			return null;
		} else {
			return "sys/403";
		}
	}
}
